﻿using System;
using System.IO;
using System.Reflection;
using System.Text;
using ProjScan.Config;
using ProjScan.RecordManager;
using ProjScan.ScanRule;
using ProjScan.ScanRule.Attr;
using ProjScan.Tools;
using UnityEngine;

namespace ProjScan.Modules
{
	// Token: 0x02000311 RID: 785
	internal class ShaderAnalysisCheckModule : BaseModules<ShaderAnalysisCheckModule, ShaderAnalysisConfig>
	{
		// Token: 0x06001286 RID: 4742 RVA: 0x00057DB8 File Offset: 0x00055FB8
		public override void Run(StreamWriter ruleStreamWriter, StreamWriter thresholdStreamWriter)
		{
			Singleton<PerformanceMeasurer>.Instance.PushState("Shader_Analysis");
			ScanLog.Instance.LogInfo(Module.Shader_Analysis, "Run");
			ToolsHelper.ShowProgressBar("Shader Analysis", "Initializing...", 0.01f, false);
			this.rulesSw = ruleStreamWriter;
			this.thresSw = thresholdStreamWriter;
			this.Init();
			this.InitRules(7000);
			RecordManagerFactory.Instance.RecordRuleInit(this.recordRules_Check);
			ScanLog.Instance.LogInfo(Module.Shader_Analysis, ScanLog.Stage.s2, "Dispatching rules done");
			try
			{
				RecordManagerFactory.Instance.GetManager<ShaderRecordManager>().PrepareAssets();
			}
			catch (Exception ex)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Shader_Analysis, ScanLog.Stage.s3, "ProjShader", string.Concat(new string[]
				{
					(ex.GetType() == null) ? null : ex.GetType().ToString(),
					"\n",
					ex.Message,
					"\n",
					ex.StackTrace
				}));
				Debug.LogError(ex.Message + "\n" + ex.StackTrace);
			}
			try
			{
				bool flag = this.shouldPrepareBuildRules;
				if (flag)
				{
					RecordManagerFactory.Instance.GetManager<ShaderBuildInfoRecordManager>().PrepareAssets();
				}
			}
			catch (Exception ex2)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Shader_Analysis, ScanLog.Stage.s3, "BuiltShader", string.Concat(new string[]
				{
					(ex2.GetType() == null) ? null : ex2.GetType().ToString(),
					"\n",
					ex2.Message,
					"\n",
					ex2.StackTrace
				}));
				Debug.LogError(ex2.Message + "\n" + ex2.StackTrace);
			}
			for (int i = 0; i < this.recordRules_Check.Count; i++)
			{
				RecordRule recordRule = this.recordRules_Check[i];
				ToolsHelper.ShowProgressBar("Output data", string.Format("Scan:{0}({1}/{2})", recordRule.GetType().Name, i + 1, this.recordRules_Check.Count), 0.5f * (float)(i + 1) / (float)this.recordRules_Check.Count, false);
				recordRule.OutputData(false);
				recordRule.OutputDetailData();
				recordRule.DumpToRuleCSV(ref this.rulesSw);
				recordRule.DumpToThresCSV(ref this.thresSw);
			}
			ScanLog.Instance.LogInfo(Module.Shader_Analysis, ScanLog.Stage.s4, "Done.");
			RecordManagerFactory.Instance.OutPutScanScr();
			ScanLog.Instance.LogInfo(Module.Shader_Analysis, ScanLog.Stage.s5, "Done.");
			RecordManagerFactory.Instance.Close();
			this.Close();
		}

		// Token: 0x06001287 RID: 4743 RVA: 0x00058090 File Offset: 0x00056290
		protected override void LoadRules(LoadMode LoadMode_0)
		{
			try
			{
				foreach (Type type in Assembly.GetExecutingAssembly().GetTypes())
				{
					bool flag = type.GetCustomAttributes(typeof(SA_Proj_RuleAttr), true).Length != 0;
					if (flag)
					{
						string text = BaseModules<ShaderAnalysisCheckModule, ShaderAnalysisConfig>.ExtractRuleName(type.FullName);
						RuleConfig ruleConfig = this.config.Get(text);
						bool checkEnabled = ruleConfig.checkEnabled;
						if (checkEnabled)
						{
							BaseRule baseRule = Activator.CreateInstance(type) as BaseRule;
							bool hasSeparateTargetDirSettings = this.config.hasSeparateTargetDirSettings;
							if (hasSeparateTargetDirSettings)
							{
								RecordRule recordRule = baseRule as RecordRule;
								recordRule.hasSeparateTargetDirSettings = true;
								recordRule.SetDirConfigs(ruleConfig.isSeparateDirSettingsEnabled, ruleConfig.targetConfig, ruleConfig.ignoreConfig);
								this.baseRules_Check.Add(recordRule);
							}
							else
							{
								this.baseRules_Check.Add(baseRule);
							}
							bool flag2 = type.IsSubclassOf(typeof(RecordRule));
							if (flag2)
							{
								RecordRule recordRule2 = baseRule as RecordRule;
								bool flag3 = LoadMode_0 == LoadMode.Check && this.config.hasWhiteList;
								if (flag3)
								{
									recordRule2.SetWhiteList(WhiteAssetManager.Instance.GetWhiteListForRuleId(text));
								}
								bool flag4 = LoadMode_0 != LoadMode.Check && this.config.hasfailedData && FailedResultManager.Instance.dicFails.ContainsKey(text);
								if (flag4)
								{
									recordRule2.failAssetInfos = FailedResultManager.Instance.dicFails[text];
								}
								this.recordRules_Check.Add(recordRule2);
							}
							else
							{
								this.globalRules_Check.Add((GlobalRule)baseRule);
							}
						}
					}
					bool flag5 = this.shouldCheckBuildRules && type.GetCustomAttributes(typeof(SA_Build_RuleAttr), true).Length != 0;
					if (flag5)
					{
						string text2 = BaseModules<ShaderAnalysisCheckModule, ShaderAnalysisConfig>.ExtractRuleName(type.FullName);
						RuleConfig ruleConfig2 = this.config.Get(text2);
						bool checkEnabled2 = ruleConfig2.checkEnabled;
						if (checkEnabled2)
						{
							this.shouldPrepareBuildRules = true;
							BaseRule baseRule2 = Activator.CreateInstance(type) as BaseRule;
							bool hasSeparateTargetDirSettings2 = this.config.hasSeparateTargetDirSettings;
							if (hasSeparateTargetDirSettings2)
							{
								RecordRule recordRule3 = baseRule2 as RecordRule;
								recordRule3.hasSeparateTargetDirSettings = true;
								recordRule3.SetDirConfigs(ruleConfig2.isSeparateDirSettingsEnabled, ruleConfig2.targetConfig, ruleConfig2.ignoreConfig);
								this.baseRules_Check.Add(recordRule3);
							}
							else
							{
								this.baseRules_Check.Add(baseRule2);
							}
							bool flag6 = type.IsSubclassOf(typeof(RecordRule));
							if (flag6)
							{
								RecordRule recordRule4 = baseRule2 as RecordRule;
								bool flag7 = LoadMode_0 == LoadMode.Check && this.config.hasWhiteList;
								if (flag7)
								{
									recordRule4.SetWhiteList(WhiteAssetManager.Instance.GetWhiteListForRuleId(text2));
								}
								bool flag8 = LoadMode_0 != LoadMode.Check && this.config.hasfailedData && FailedResultManager.Instance.dicFails.ContainsKey(text2);
								if (flag8)
								{
									recordRule4.failAssetInfos = FailedResultManager.Instance.dicFails[text2];
								}
								this.recordRules_Check.Add(recordRule4);
							}
							else
							{
								this.globalRules_Check.Add((GlobalRule)baseRule2);
							}
						}
					}
				}
			}
			catch (Exception ex)
			{
				ScanLog.Instance.LogInfo(ScanLog.LogLevel.EXCEPTION, Module.Unknown, "Load Rules", string.Concat(new string[]
				{
					(ex.GetType() == null) ? null : ex.GetType().ToString(),
					"\n",
					ex.Message,
					"\n",
					ex.StackTrace
				}));
			}
			StringBuilder stringBuilder = new StringBuilder();
			foreach (BaseRule baseRule3 in this.baseRules_Check)
			{
				stringBuilder.Append(baseRule3.GetType().Name).Append(", ");
			}
			ScanLog.Instance.LogInfo(Module.Unknown, ScanLog.Stage.s1, "EnabledRules", stringBuilder.ToString());
		}

		// Token: 0x06001288 RID: 4744 RVA: 0x000584D4 File Offset: 0x000566D4
		public override void Init()
		{
			this.config = (Singleton<ConfigMgr>.Instance.GetConfig<ShaderAnalysisConfig>() as ShaderAnalysisConfig);
			this.shouldPrepareBuildRules = false;
			bool flag = (Singleton<ScanInfo>.Instance.unityVersion[0] >= 2018 && Singleton<ScanInfo>.Instance.unityVersion[1] >= 2) || Singleton<ScanInfo>.Instance.unityVersion[0] >= 2019;
			if (flag)
			{
				this.shouldCheckBuildRules = true;
			}
			else
			{
				this.shouldCheckBuildRules = false;
			}
			this.baseRules_Check.Clear();
			this.recordRules_Check.Clear();
			this.globalRules_Check.Clear();
			RecordManagerFactory.Instance.Init();
			this.LoadRules(LoadMode.Check);
		}

		// Token: 0x06001289 RID: 4745 RVA: 0x00008AA0 File Offset: 0x00006CA0
		public override void Close()
		{
			Singleton<PerformanceMeasurer>.Instance.CloseAndRecordState();
			Singleton<PerformanceMeasurer>.Instance.AddExecutionCheckpoint("ShaderAnalysisEnd");
			ScanLog.Instance.LogInfo(Module.Shader_Analysis, ScanLog.Stage.s6, "Done.");
		}

		// Token: 0x04000AF7 RID: 2807
		public bool shouldCheckBuildRules = true;

		// Token: 0x04000AF8 RID: 2808
		public bool shouldPrepareBuildRules = false;
	}
}
